
 
import { BoostResourceConfig } from "../constants/constants";
import {   Roles } from "./setups";

// 角色的强化行为配置
// 角色:{行为:行为对应的body强化个数}  -1.就是强化全部
export const BoostSetup:Partial< Record<RoleName,{[action:string]:number}>>=
{
    upgrader:{
        upgradeController:-1,
    },
    worker:{
        build:-1,
    },
    mine:{
        harvest:-1,
    },
    // [Roles.depo]:{
    //     harvest:-1,
    // },
    // [Roles.t_d]:{
    //     dismantle:-1,
    //     damage:-1,
    //     fatigue:-1,
    // },
    // [Roles.t_h]:{
    //     heal:-1,
    //     damage:-1,
    //     fatigue:-1,
    // },
    // [Roles.pb_a]:{
    //     attack:-1,
    //     damage:-1   // 减伤
    // },
    // [Roles.pb_h]:{
    //     heal:-1,
    // },
    // [Roles.t_ra]:{
    //     rangedAttack:-1,
    //     heal:-1,
    //     damage:-1,
    //     fatigue:-1,
    // },
    // [Roles.t2_h]:{
    //     attack:-1,
    //     fatigue:-1,
    //     damage:-1,
    //     heal:-1,
    // },
    // [Roles.t2_a]:{
    //     attack:-1,
    //     fatigue:-1,
    //     damage:-1,
    //     heal:-1,
    // },
}  

export type BoostMap={[part:string]:{ [action: string]:Array<{[boost:string]:number}> }}
export class CreepBoostSetup
{
    private static BoostMap:BoostMap;
    private static Actionmap:{[action:string]:string};

    public static  BoostCreepConfig(creep:Creep,boostLevel:BoostLevel):(BoostResourceConfig&{BodyCount?:number})[]
    {
        if(boostLevel>3 || boostLevel<1)
        {
            throw new Error("boostLevel must be 1,2,3");
        }
        const bodygroup  = _.groupBy(creep.body,item=>item.type);
        const actionconfig = BoostSetup[creep.memory.Role];

        if(actionconfig)
        {
            // 1.有配置使用配置定义的Action
            const boostconfigs:(BoostResourceConfig&{BodyCount?:number})[]=[];
            for(const action  in actionconfig)
            {
               const bodypart = CreepBoostSetup.GetActionmap()[action] as BodyPartConstant;
               // 如果存在Bodypart则生成强化表
               if(bodygroup[bodypart])
               {
                   const boost = this.BoostConstant(bodypart,action as CreepAction,boostLevel);
                   const boostCount = actionconfig[action] ==-1?bodygroup[bodypart].length:actionconfig[action];

                   const boostcfg={resource:boost,amount:boostCount*30,BodyCount:boostCount};
                   boostconfigs.push(boostcfg);
               }
            }
            return boostconfigs;
        }
        else
        {

            // 2.没有配置就使用part第一个的action作为强化
            const boostconfigs:(BoostResourceConfig&{BodyCount?:number})[]=[];
            for(const part in bodygroup)
            {
                const action =CreepBoostSetup.DefaultAction(part as BodyPartConstant);
                {
                    const boost = this.BoostConstant(part as BodyPartConstant,action as CreepAction,boostLevel);
                    const boostCount =  bodygroup[part].length;

                    const boostcfg={resource:boost,amount:boostCount*30,BodyCount:boostCount};
                    boostconfigs.push(boostcfg);
                }
            }
            // Memory.Debug[`${creep.name}使用默认的配置强化行为`]= boostconfigs;
            return boostconfigs;
        }

    }

    public static  getBoostMinerials(bodypartset:Set<BodyPartConstant>,role:RoleName,boostLevel:BoostLevel):MineralConstant[]
    {
        if(boostLevel>3 || boostLevel<1)
        {
            throw new Error("boostLevel must be 1,2,3");
        }
        // const bodypartset:Set<BodyPartConstant> = new Set<BodyPartConstant>();
        // Object.keys(creep.pattern).forEach(e=>bodypartset.add(e as BodyPartConstant));
        // Object.keys(creep.constbody).forEach(e=>bodypartset.add(e as BodyPartConstant));

        const actionconfig = BoostSetup[role];
        const boost_minerilas : MineralConstant[]=[];
        if(actionconfig)
        {
            // 1.有配置使用配置定义的Action
            const boostconfigs:(BoostResourceConfig&{BodyCount?:number})[]=[];
            for(const action  in actionconfig)
            {
               const bodypart = CreepBoostSetup.GetActionmap()[action] as BodyPartConstant;
               // 如果存在Bodypart则生成强化表
               const minerialconst = this.BoostConstant(bodypart,action as CreepAction,boostLevel) as MineralConstant;
               boost_minerilas.push(minerialconst);
            }
            return boost_minerilas;
        }
        else
        {

            // 2.没有配置就使用part第一个的action作为强化
            const boostconfigs:(BoostResourceConfig&{BodyCount?:number})[]=[];
            bodypartset.forEach(e=>{
                const action =CreepBoostSetup.DefaultAction(e);
                const minerialconst = this.BoostConstant(e,action as CreepAction,boostLevel) as MineralConstant;
                boost_minerilas.push(minerialconst);
            })

            return boost_minerilas;
        }

    }


     public static BoostConstant(part:BodyPartConstant,action:CreepAction,boostlevel:BoostLevel):MineralBoostConstant
     {
        const boostcfg =  CreepBoostSetup.BOOSTS()[part][action][boostlevel-1];
       // global.Log.Info(`part:${part}  action:${action} boostconfig:${JSON.stringify(CreepBoostSetup.BOOSTS())}`);
        const key =  Object.keys(boostcfg)[0];
        return key as MineralBoostConstant;
     }
     public static BoostConstants(part:BodyPartConstant,action:CreepAction):MineralBoostConstant[]
     {
        const boostcfg =  CreepBoostSetup.BOOSTS()[part][action] ;
       // global.Log.Info(`part:${part}  action:${action} boostconfig:${JSON.stringify(CreepBoostSetup.BOOSTS())}`);
       return _.map(boostcfg,e=> Object.keys(e)[0]) as MineralBoostConstant[];

     }
     private static DefaultAction(part:BodyPartConstant):CreepAction
     {
        const actions =  CreepBoostSetup.BOOSTS()[part];

        const firstaction = Object.keys(actions)[0];
        return firstaction as CreepAction;
     }
     private static BOOSTS():BoostMap
     {
        if(CreepBoostSetup.BoostMap)
        {
            return CreepBoostSetup.BoostMap;
        }
        const BODYBOOSTS:BoostMap={};
        for(const key in BOOSTS)
        {
            console.log(key);
            const boosts = BOOSTS[key];
            const action_map2: { [action: string]:Array<{[boost:string]:number}> } = {};

            for(const boost in boosts)
            {
                const action_boosts = boosts[boost];
                for(const action in action_boosts)
                {
                    if(!action_map2[action])
                    {
                        action_map2[action] = [];
                    }
                    action_map2[action].push({[boost]:action_boosts[action] } );
                }
            }
            BODYBOOSTS[key] = action_map2;
        }
        CreepBoostSetup.BoostMap = BODYBOOSTS;
        return BODYBOOSTS;
     }

     private static GetActionmap():Readonly <{[action:string]:string}>
     {
        if(CreepBoostSetup.Actionmap)
        {
            return CreepBoostSetup.Actionmap;
        }
        const actionmap:{[action:string]:string}={};
        for(const key in BOOSTS)
        {
            console.log(key);
            const boosts = BOOSTS[key];
            for(const boost in boosts)
            {
                const action_boosts = boosts[boost];
                for(const action in action_boosts)
                {
                    if(actionmap[action])
                    {
                      break;
                    }
                    actionmap[action] = key;
                }
            }
        }
        CreepBoostSetup.Actionmap = actionmap;
        return CreepBoostSetup.Actionmap;
     }
}
